/************************************************************************************/
/*  Program name CAPGLIMP_AUTOPOST_OPTION.sql                                       */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 25 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_AUTOPOST_OPTION';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_AUTO_POSTING_OPTS;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO GL_AUTOMATIC_POSTING_OPTIONS
			VALUES
			(
			CAPBRIMP_MAPPING.GetGLAutoPostSetIDMapping(C1.autopost_set_id),
			C1.actual_flag,
			C1.period_name,
			C1.je_source_name,
			C1.je_category_name,
			C1.posting_priority,
			C1.last_update_date,
			CAPBRIMP_COMMON.GetUser(C1.last_updated_name),
			C1.creation_date,
			CAPBRIMP_COMMON.GetUser(C1.created_name),
			C1.last_update_login,
			C1.attribute1,
			C1.attribute2,
			C1.attribute3,
			C1.attribute4,
			C1.attribute5,
			C1.attribute6,
			C1.attribute7,
			C1.attribute8,
			C1.attribute9,
			C1.attribute10,
			C1.attribute11,
			C1.attribute12,
			C1.attribute13,
			C1.attribute14,
			C1.attribute15,
			C1.context
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/